package com.net.dao.sell.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Component;

import com.net.dao.SuperDao;
import com.net.dao.sell.SellBackMainDao;
import com.net.model.Commodity;
import com.net.model.Customer;
import com.net.model.SellBackDetail;
import com.net.model.SellBackMain;
@Component("sellBackMainDao")
public class SellBackMainDaoImpl extends SuperDao implements SellBackMainDao {

	public boolean delete(SellBackMain sellBackMain) {
		SellBackMain sellBack = (SellBackMain) this.getHibernateTemplate().load(SellBackMain.class, sellBackMain.getId());
		if(sellBack!=null){
			List<SellBackDetail> sellBackDetails = this.getHibernateTemplate().loadAll(SellBackDetail.class);
			List<SellBackDetail> sellBacks = new ArrayList<SellBackDetail>();
			for(int i = 0; i < sellBackDetails.size(); i ++) {
				if(sellBackDetails.get(i).getSellBackMain().getId() == sellBack.getId());
					sellBacks.add(sellBackDetails.get(i));
			}
			this.getHibernateTemplate().deleteAll(sellBacks);
			this.getHibernateTemplate().delete(sellBack);
			return true;
		}else
			return false;
	}

	@SuppressWarnings("unchecked")
	public int getCount() {
		HibernateCallback hc = new HibernateCallback(){

			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				return session.createQuery("from SellBackMain").list();
			}
			
		};
		return getHibernateTemplate().executeFind(hc).size();
	}

	@SuppressWarnings("unchecked")
	public List<SellBackMain> getSellBackMains(final int currentPage, final int perPage,
			final String orderType, final String orderName) {
		HibernateCallback hc = new HibernateCallback(){
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				
				if(orderType==null||orderName==null){
				return session.createQuery("from SellBackMain").setFirstResult(currentPage*perPage).setMaxResults(perPage).list();
				}else{
					System.out.println("from SellBackMain order by "+orderName+" "+ orderType);
					return session.createQuery("from SellBackMain order by "+orderName+" "+ orderType).setFirstResult(currentPage*perPage).setMaxResults(perPage).list();
				}
			}
		};
		
		System.out.println(getHibernateTemplate().executeFind(hc));
		return (List<SellBackMain>)getHibernateTemplate().execute(hc);
	}

	@SuppressWarnings("unchecked")
	public List<SellBackMain> getSellBackMainsByCondition(final int currentPage,
			final int perPage, final String condition) {
		HibernateCallback hc = new HibernateCallback(){
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				String str = "from SellBackMain S where " + condition;
				System.out.println(str);
				return session.createQuery(str).setFirstResult(currentPage*perPage).setMaxResults(perPage).list();
			}
			
		};
		return getHibernateTemplate().executeFind(hc);
	}

	public SellBackMain loadSellBackMainById(int id) {
		return (SellBackMain) this.getHibernateTemplate().load(SellBackMain.class, id);
	}

	@SuppressWarnings("unchecked")
	public SellBackMain loadSellBackMainByName(String name) {
		List<SellBackMain> sellBackMains = getHibernateTemplate().findByCriteria(DetachedCriteria.forClass(SellBackMain.class).add(Restrictions.eq("name", name)));
		if(sellBackMains!=null&&sellBackMains.size()>0){
			return sellBackMains.get(0);
		}else
			return null;
	}

	public void save(SellBackMain sellBackMain) {
		getHibernateTemplate().save(sellBackMain);
	}

	public void update(SellBackMain sellBackMain) {
		getHibernateTemplate().saveOrUpdate(sellBackMain);
	}
	
	/****/
	@SuppressWarnings("unchecked")
	public List<SellBackMain> getSellBackMains() {
		System.out.println("to find !");
		@SuppressWarnings("unused")
		List<SellBackMain> list = this.getHibernateTemplate().find("from SellBackMain");
		return (List<SellBackMain>)this.getHibernateTemplate().find("from SellBackMain");
	}

	@SuppressWarnings("unchecked")
	public Commodity getCommodityByName(String name) {
		List<Commodity> Commoditys = getHibernateTemplate().findByCriteria(DetachedCriteria.forClass(Commodity.class).add(Restrictions.eq("commodity_name", name)));
		if(Commoditys!=null&&Commoditys.size()>0){
			return Commoditys.get(0);
		}else
			return null;
	}

	@SuppressWarnings("unchecked")
	public Customer getCustomerByName(String customerName) {
		List<Customer> Customers = getHibernateTemplate().findByCriteria(DetachedCriteria.forClass(Customer.class).add(Restrictions.eq("customer_name", customerName)));
		if(Customers!=null&&Customers.size()>0){
			return Customers.get(0);
		}else
			return null;
	}

	public void add(SellBackDetail sb) {
		getHibernateTemplate().save(sb);
	}

	public void save(SellBackMain sellBackMain, List<Commodity> commoditys,
			Customer customer, List<SellBackDetail> sellBackDetails) {
		float price = 0;
		for(int i = 0; i < commoditys.size(); i ++) {
			String name = commoditys.get(i).getCommodity_name(); 
			Commodity commodity = this.getCommodityByName(name); 
			price += commodity.getCurrent_price() * sellBackDetails.get(i).getQuantity(); 
		}
		System.out.println("customer:　" + customer.getCustomer_name()); 
		customer = this.getCustomerByName(customer.getCustomer_name());
		sellBackMain.setCustomer(customer); 
		sellBackMain.setTotalMoney(price);
		sellBackMain.setTypeCount(commoditys.size());
		sellBackMain.setSellbackDetail(null);
		System.out.println("9090");
		getHibernateTemplate().save(sellBackMain);
		
		/*for(int i = 0 ; i < commoditys.size(); i++) { 
			String name = commoditys.get(i).getCommodity_name(); 
			Commodity commodity = this.getCommodityByName(name); 
			SellBackDetail sb = new SellBackDetail(); 
			sb = sellBackDetails.get(i);
		 	sb.setCommodity(commodity); 
		 	sb.setSellBackMain(sellBackMain);
		 	this.add(sb); 
		 }*/
	}

	public SellBackMain getSellBackMainById(int id) {
		return (SellBackMain) getHibernateTemplate().get(SellBackMain.class, id);
	}

	public void update(SellBackMain sellBackMain, List<Commodity> commoditys,
			Customer customer, List<SellBackDetail> sellBackDetails) {
		float price = 0;
		for(int i = 0; i < commoditys.size(); i ++) {
			String name = commoditys.get(i).getCommodity_name(); 
			Commodity commodity = this.getCommodityByName(name); 
			price += commodity.getCurrent_price() * sellBackDetails.get(i).getQuantity(); 
		}
		System.out.println("customer:　" + customer.getCustomer_name()); 
		customer = this.getCustomerByName(customer.getCustomer_name());
		sellBackMain.setCustomer(customer); sellBackMain.setTotalMoney(price);
		sellBackMain.setTypeCount(commoditys.size());
		getHibernateTemplate().update(sellBackMain);
		
		for(int i = 0 ; i < commoditys.size(); i++) { 
			String name = commoditys.get(i).getCommodity_name(); 
			Commodity commodity = this.getCommodityByName(name); 
			SellBackDetail sb = new SellBackDetail(); 
			sb = sellBackDetails.get(i);
		 	sb.setCommodity(commodity); 
		 	sb.setSellBackMain(sellBackMain);
		 	getHibernateTemplate().update(sb); 
		 }
	}

}
